package com.hkts.reportform.datastatics.serviceImpl;

import com.google.gson.Gson;
import com.hkts.reportform.datastatics.dao.ReportDao;
import com.hkts.reportform.datastatics.entity.ReportForm;
import com.hkts.reportform.datastatics.service.ReportService;
import com.hkts.reportform.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ClassNameReportServiceImpl
 * @Description
 * @Author yck
 * @Date2020-10-19 11:25
 * @Version V1.0
 **/
@Service("reportServiceImpl")
public class ReportServiceImpl implements ReportService {
    @Autowired
    private ReportDao reportDaoImpl;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<Map<String, Object>> selectInventory() {

        return reportDaoImpl.selectInventory();
    }

    //沥青数据同步到redis
    @Override
    public void asphaltData() throws Exception {
        List<String> dateArray = DateUtil.getDateArray(7);
        List<Float> consumeList = new ArrayList<>();
        List<Float> stockList = new ArrayList<>();
        for (int i = 0; i < dateArray.size(); i++) {
            List<ReportForm> storedProcedure = reportDaoImpl.getStoredProcedure(dateArray.get(i), dateArray.get(i), InventoryClassFildes.ASPHALT);
            stockList.add(storedProcedure.get(0).getStockOnHand());
            consumeList.add(storedProcedure.get(0).getUsage());
        }
        redisTemplate.delete(ErpProperties.ASPHALT_STOCK);
        redisTemplate.delete(ErpProperties.ASPHALT_CONSUME);
        redisTemplate.opsForList().leftPushAll(ErpProperties.ASPHALT_STOCK, stockList);
        redisTemplate.opsForList().leftPushAll(ErpProperties.ASPHALT_CONSUME, consumeList);
    }

    //昨日石油焦消耗数据
    @Override
    public void petroleumCokeData() throws Exception {
        //石油焦
        SimpleDateFormat ss = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat monthFormat = new SimpleDateFormat("yyyy-MM");
        List<ReportForm> storedProcedure = reportDaoImpl.getStoredProcedure(
                ss.format(DateUtil.getYesterday(7)), ss.format(DateUtil.getYesterday()),
                InventoryClassFildes.PETROLEUM_COKE + "\t" + InventoryClassFildes.GREEN_BODY + "\t" + InventoryClassFildes.COOKED_BILLET);
        Float petroleumCoke = (float) 0;
        Float greenbody = (float) 0;
        Float cookedBillet = (float) 0;
        List<Float> stockList = new ArrayList<>();
        for (int i = 0; i < storedProcedure.size(); i++) {
            if (storedProcedure.get(i).getStockCode().startsWith(InventoryClassFildes.PETROLEUM_COKE)) {
                petroleumCoke = petroleumCoke + storedProcedure.get(i).getUsage();
            } else if (storedProcedure.get(i).getStockCode().startsWith(InventoryClassFildes.GREEN_BODY)) {
                greenbody = greenbody + storedProcedure.get(i).getYield();
            } else if (storedProcedure.get(i).getStockCode().startsWith(InventoryClassFildes.COOKED_BILLET)) {
                cookedBillet = cookedBillet + storedProcedure.get(i).getYield();
            }
        }
        redisTemplate.opsForValue().set(ErpProperties.PETROLEUM_COKE_WEEK, petroleumCoke);
        redisTemplate.opsForValue().set(ErpProperties.GREEN_BODY_WEEK, greenbody);
        redisTemplate.opsForValue().set(ErpProperties.COOKED_BILLET_WEEK, cookedBillet);
        //本月消耗生产
        String format = monthFormat.format(new Date());
        List<ReportForm> storedProcedureMonth = reportDaoImpl.getStoredProcedure(format + "-01",
                ss.format(new Date()),
                InventoryClassFildes.PETROLEUM_COKE + "\t" + InventoryClassFildes.GREEN_BODY + "\t" + InventoryClassFildes.COOKED_BILLET);
        Float petroleumCokeMonth = (float) 0;
        Float greenbodyMonth = (float) 0;
        Float cookedBilletMonth = (float) 0;
        for (int i = 0; i < storedProcedureMonth.size(); i++) {
            if (storedProcedureMonth.get(i).getStockCode().startsWith(InventoryClassFildes.PETROLEUM_COKE)) {
                petroleumCokeMonth = petroleumCokeMonth + storedProcedureMonth.get(i).getUsage();
            } else if (storedProcedureMonth.get(i).getStockCode().startsWith(InventoryClassFildes.GREEN_BODY)) {
                greenbodyMonth = greenbodyMonth + storedProcedureMonth.get(i).getYield();
            } else if (storedProcedureMonth.get(i).getStockCode().startsWith(InventoryClassFildes.COOKED_BILLET)) {
                cookedBilletMonth = cookedBilletMonth + storedProcedureMonth.get(i).getYield();
            }
        }
        redisTemplate.opsForValue().set(ErpProperties.PETROLEUM_COKE_MONTH, petroleumCokeMonth);
        redisTemplate.opsForValue().set(ErpProperties.GREEN_BODY_MONTH, greenbodyMonth);
        redisTemplate.opsForValue().set(ErpProperties.COOKED_BILLET_MONTH, cookedBilletMonth);
    }

    //石油焦雷达图
    @Override
    public void radarChartOfpetroleumCoke() throws Exception {
        SimpleDateFormat ss = new SimpleDateFormat("yyyy-MM-dd");
        List<ReportForm> storedProcedure = reportDaoImpl.getStoredProcedure(ss.format(new Date()),
                ss.format(new Date()),
                InventoryClassFildes.PETROLEUM_COKE);
        List<String> listName = new ArrayList<>();
        List<Float> listValue = new ArrayList<>();
        for (int i = 0; i < storedProcedure.size(); i++) {
            if (storedProcedure.get(i).getStockOnHand() > 0) {
                listName.add(storedProcedure.get(i).getStockName());
                listValue.add(storedProcedure.get(i).getStockOnHand());
            }
        }
        redisTemplate.delete(ErpProperties.PETROLEUM_COKE_RATE_NAMES);
        redisTemplate.delete(ErpProperties.PETROLEUM_COKE_RATE_VALUES);
        redisTemplate.opsForList().leftPushAll(ErpProperties.PETROLEUM_COKE_RATE_NAMES, listName);
        redisTemplate.opsForList().leftPushAll(ErpProperties.PETROLEUM_COKE_RATE_VALUES, listValue);
    }

    //煅后焦消耗
    @Override
    public void consumptionAnalysisOfCalcinedCoke() throws Exception {
        List<String> dateArray = DateUtil.getDateArray(7);
        List<Float> listValue = new ArrayList<>();
        for (int i = 0; i < dateArray.size(); i++) {
            List<ReportForm> storedProcedure = reportDaoImpl.getStoredProcedure(dateArray.get(i),
                    dateArray.get(i),
                    InventoryClassFildes.CALCINED_COKE);
            Float temp = (float) 0;
            for (int j = 0; j < storedProcedure.size(); j++) {
                temp = temp + storedProcedure.get(j).getUsage();
            }
            listValue.add(temp);
        }
        redisTemplate.delete(ErpProperties.CALCINED_COKE_WEEK_CONSUME);
        redisTemplate.opsForList().leftPushAll(ErpProperties.CALCINED_COKE_WEEK_CONSUME, listValue);
    }

    //辅材类别对比
    @Override
    public void categoryOfauxiliaryMaterials() throws Exception {
        Gson gson = new Gson();
        List<Map<String, Object>> maps = reportDaoImpl.auxiliaryMaterialslist();
        String inventoryClassStrByList = InventoryClassUtils.getInventoryClassStrByList(maps);
        SimpleDateFormat ss = new SimpleDateFormat("yyyy-MM-dd");
        List<ReportForm> storedProcedure = reportDaoImpl.getStoredProcedure(ss.format(DateUtil.getYesterday()),
                ss.format(DateUtil.getYesterday()),
                inventoryClassStrByList);
        List<Float> listDay = new ArrayList<>();
        Float mechanicsDay = (float) 0;
        Float electricleDay = (float) 0;
        Float materialScienceDay = (float) 0;
        Float officeSuppliesDay = (float) 0;
        Float infrastructreDay = (float) 0;
        Float otherDay = (float) 0;

        for (int i = 0; i < storedProcedure.size(); i++) {
            if (storedProcedure.get(i).getStockCode().startsWith(InventoryClassFildes.MECHANICS)) {
                mechanicsDay = mechanicsDay + storedProcedure.get(i).getUsage();
            } else if (storedProcedure.get(i).getStockCode().startsWith(InventoryClassFildes.ELECTRICLE)) {
                electricleDay = electricleDay + storedProcedure.get(i).getUsage();
            } else if (storedProcedure.get(i).getStockCode().startsWith(InventoryClassFildes.MATERIAL_SCIENCE)) {
                materialScienceDay = materialScienceDay + storedProcedure.get(i).getUsage();
            } else if (storedProcedure.get(i).getStockCode().startsWith(InventoryClassFildes.OFFICE_SUPPLIES)) {
                officeSuppliesDay = officeSuppliesDay + storedProcedure.get(i).getUsage();
            } else if (storedProcedure.get(i).getStockCode().startsWith(InventoryClassFildes.INFRASTRUCTRE)) {
                infrastructreDay = infrastructreDay + storedProcedure.get(i).getUsage();
            } else if (storedProcedure.get(i).getStockCode().startsWith(InventoryClassFildes.OTHER)) {
                otherDay = otherDay + storedProcedure.get(i).getUsage();
            }
        }
        listDay.add(mechanicsDay);
        listDay.add(electricleDay);
        listDay.add(materialScienceDay);
        listDay.add(officeSuppliesDay);
        listDay.add(infrastructreDay);
        listDay.add(otherDay);
        redisTemplate.delete(ErpProperties.AUXILIARY_MATERIALS_DAY);
        redisTemplate.opsForList().leftPushAll(ErpProperties.AUXILIARY_MATERIALS_DAY, listDay);
        //日排序
        redisTemplate.opsForValue().set(ErpProperties.AUXILIARY_MATERIALS_SORT_DAY, gson.toJson(SortUtils.sortLiet(storedProcedure)));
        //辅料周数据
        List<Float> listWeek = new ArrayList<>();
        Float mechanicsWeek = (float) 0;
        Float electricleWeek = (float) 0;
        Float materialScienceWeek = (float) 0;
        Float officeSuppliesWeek = (float) 0;
        Float infrastructreWeek = (float) 0;
        Float otherWeek = (float) 0;
        Date yesterday = DateUtil.getYesterday(2);
        List<ReportForm> storedProcedureWeek = reportDaoImpl.getStoredProcedure(ss.format(DateUtil.getYesterday(7)),
                ss.format(DateUtil.getYesterday()),
                inventoryClassStrByList);
        for (int i = 0; i < storedProcedureWeek.size(); i++) {
            if (storedProcedureWeek.get(i).getStockCode().startsWith(InventoryClassFildes.MECHANICS)) {
                mechanicsWeek = mechanicsWeek + storedProcedureWeek.get(i).getUsage();
            } else if (storedProcedureWeek.get(i).getStockCode().startsWith(InventoryClassFildes.ELECTRICLE)) {
                electricleWeek = electricleWeek + storedProcedureWeek.get(i).getUsage();
            } else if (storedProcedureWeek.get(i).getStockCode().startsWith(InventoryClassFildes.MATERIAL_SCIENCE)) {
                materialScienceWeek = materialScienceWeek + storedProcedureWeek.get(i).getUsage();
            } else if (storedProcedureWeek.get(i).getStockCode().startsWith(InventoryClassFildes.OFFICE_SUPPLIES)) {
                officeSuppliesWeek = officeSuppliesWeek + storedProcedureWeek.get(i).getUsage();
            } else if (storedProcedureWeek.get(i).getStockCode().startsWith(InventoryClassFildes.INFRASTRUCTRE)) {
                infrastructreWeek = infrastructreWeek + storedProcedureWeek.get(i).getUsage();
            } else if (storedProcedureWeek.get(i).getStockCode().startsWith(InventoryClassFildes.OTHER)) {
                otherWeek = otherWeek + storedProcedureWeek.get(i).getUsage();
            }
        }
        listWeek.add(mechanicsWeek);
        listWeek.add(electricleWeek);
        listWeek.add(materialScienceWeek);
        listWeek.add(officeSuppliesWeek);
        listWeek.add(infrastructreWeek);
        listWeek.add(otherWeek);
        redisTemplate.delete(ErpProperties.AUXILIARY_MATERIALS_WEEK);
        redisTemplate.opsForList().leftPushAll(ErpProperties.AUXILIARY_MATERIALS_WEEK, listWeek);
        //周排序
        redisTemplate.opsForValue().set(ErpProperties.AUXILIARY_MATERIALS_SORT_WEEK, gson.toJson(SortUtils.sortLiet(storedProcedureWeek)));
        //月数据
        List<Float> listMonth = new ArrayList<>();
        Float mechanicsMonth = (float) 0;
        Float electricleMonth = (float) 0;
        Float materialScienceMonth = (float) 0;
        Float officeSuppliesMonth = (float) 0;
        Float infrastructreMonth = (float) 0;
        Float otherMonth = (float) 0;
        SimpleDateFormat fomatMonth = new SimpleDateFormat("yyyy-MM");
        List<ReportForm> storedProceduremonth = reportDaoImpl.getStoredProcedure(fomatMonth.format(new Date()) + "-01",
                ss.format(DateUtil.getYesterday()),
                inventoryClassStrByList);
        for (int i = 0; i < storedProceduremonth.size(); i++) {
            if (storedProceduremonth.get(i).getStockCode().startsWith(InventoryClassFildes.MECHANICS)) {
                mechanicsMonth = mechanicsMonth + storedProceduremonth.get(i).getUsage();
            } else if (storedProceduremonth.get(i).getStockCode().startsWith(InventoryClassFildes.ELECTRICLE)) {
                electricleMonth = electricleMonth + storedProceduremonth.get(i).getUsage();
            } else if (storedProceduremonth.get(i).getStockCode().startsWith(InventoryClassFildes.MATERIAL_SCIENCE)) {
                materialScienceMonth = materialScienceMonth + storedProceduremonth.get(i).getUsage();
            } else if (storedProceduremonth.get(i).getStockCode().startsWith(InventoryClassFildes.OFFICE_SUPPLIES)) {
                officeSuppliesMonth = officeSuppliesMonth + storedProceduremonth.get(i).getUsage();
            } else if (storedProceduremonth.get(i).getStockCode().startsWith(InventoryClassFildes.INFRASTRUCTRE)) {
                infrastructreMonth = infrastructreMonth + storedProceduremonth.get(i).getUsage();
            } else if (storedProceduremonth.get(i).getStockCode().startsWith(InventoryClassFildes.OTHER)) {
                otherMonth = otherMonth + storedProceduremonth.get(i).getUsage();
            }
        }
        listMonth.add(mechanicsMonth);
        listMonth.add(electricleMonth);
        listMonth.add(materialScienceMonth);
        listMonth.add(officeSuppliesMonth);
        listMonth.add(infrastructreMonth);
        listMonth.add(otherMonth);
        redisTemplate.delete(ErpProperties.AUXILIARY_MATERIALS_MONTH);
        redisTemplate.opsForList().leftPushAll(ErpProperties.AUXILIARY_MATERIALS_MONTH, listMonth);
        //月排序
        redisTemplate.opsForValue().set(ErpProperties.AUXILIARY_MATERIALS_SORT_MONTH, gson.toJson(SortUtils.sortLiet(storedProceduremonth)));
    }

    //熟坯库存
    @Override
    public void cookedBillet() throws Exception {
        SimpleDateFormat ss = new SimpleDateFormat("yyyy-MM-dd");
        List<ReportForm> storedProcedureWeek = reportDaoImpl.getStoredProcedure(ss.format(DateUtil.getYesterday(7)),
                ss.format(DateUtil.getYesterday()),
                InventoryClassFildes.COOKED_BILLET);
        List<String> listName = new ArrayList<>();
        List<Float> listValue = new ArrayList<>();
        for (int i = 0; i < storedProcedureWeek.size(); i++) {
            if (storedProcedureWeek.get(i).getStockOnHand() > 0) {
                listName.add(storedProcedureWeek.get(i).getStockName());
                listValue.add(storedProcedureWeek.get(i).getStockOnHand());
            }
        }
        redisTemplate.delete(ErpProperties.COOKES_BILLET_NAME);
        redisTemplate.opsForList().leftPushAll(ErpProperties.COOKES_BILLET_NAME, listName);
        redisTemplate.delete(ErpProperties.COOKES_BILLET_VALUE);
        redisTemplate.opsForList().leftPushAll(ErpProperties.COOKES_BILLET_VALUE, listValue);
    }

    @Override
    public void cookeBilletCurve() throws Exception {
        SimpleDateFormat ss = new SimpleDateFormat("yyyy-MM-dd");
        List<ReportForm> listButtons = new ArrayList<>();
        Gson gson = new Gson();
        List<String> listCode = new ArrayList<>();
        List<String> dateArray = DateUtil.getDateArray(7);
        List<ReportForm> storedProcedureWeek = reportDaoImpl.getStoredProcedure(ss.format(DateUtil.getYesterday(7)),
                ss.format(DateUtil.getYesterday()), InventoryClassFildes.COOKED_BILLET);
        for (int i = 0; i < storedProcedureWeek.size(); i++) {
            if (storedProcedureWeek.get(i).getYield() > 0) {
                listButtons.add(storedProcedureWeek.get(i));
                listCode.add(storedProcedureWeek.get(i).getStockCode());
                redisTemplate.delete(redisTemplate.delete(ErpProperties.COOKED_BILLET_WEEK_CODE + storedProcedureWeek.get(i).getStockCode()));
            }
        }
        redisTemplate.opsForValue().set(ErpProperties.COOKED_BILLET_BUTTONS, gson.toJson(listButtons));
        redisTemplate.delete(ErpProperties.COOKED_BILLET_CODES);
        redisTemplate.opsForList().leftPushAll(ErpProperties.COOKED_BILLET_CODES, listCode);

        for (int i = 0; i < dateArray.size(); i++) {
            List<ReportForm> storedProcedure = reportDaoImpl.getStoredProcedure(dateArray.get(i),
                    dateArray.get(i), InventoryClassFildes.COOKED_BILLET);
            for (int j = 0; j < storedProcedure.size(); j++) {
                if (listCode.contains(storedProcedure.get(j).getStockCode())) {
                    redisTemplate.opsForList().leftPushAll(ErpProperties.COOKED_BILLET_WEEK_CODE + storedProcedure.get(j).getStockCode(), storedProcedure.get(j).getYield());
                }
            }
        }
    }

    //现存量数据初始化
    @Override
    public void stockDataInit() throws Exception {
        String beginDate = "2020-07-01";
        SimpleDateFormat ss=new SimpleDateFormat("yyyy-MM-dd");
        String endDate = ss.format(DateUtil.getYesterday());
        String[] dateArray={beginDate,endDate};
        List<String> daysByTime = DateUtil.getDaysByTime(dateArray);
        for(int i=0;i<daysByTime.size();i++){
            Set keys = redisTemplate.opsForHash().keys(ErpProperties.WECHAT_STOCK + daysByTime.get(i));
            if(keys.size()==0){
                List<ReportForm> storedProcedure = reportDaoImpl.getStoredProcedure(daysByTime.get(i), daysByTime.get(i), InventoryClassFildes.PETROLEUM_COKE+"\t"+InventoryClassFildes.ASPHALT);
                for(int j=0;j<storedProcedure.size();j++){
                    redisTemplate.opsForHash().put(ErpProperties.WECHAT_STOCK+daysByTime.get(i),storedProcedure.get(j).getStockCode(),storedProcedure.get(j).getStockOnHand());
                }
            }
            System.out.println(i);
        }

    }

    @Override
    public void stockDataUpdate() throws Exception {
        SimpleDateFormat ss=new SimpleDateFormat("yyyy-MM-dd");
        Date yesterday = DateUtil.getYesterday();
        List<ReportForm> storedProcedure = reportDaoImpl.getStoredProcedure(ss.format(yesterday),ss.format(yesterday), InventoryClassFildes.PETROLEUM_COKE+"\t"+InventoryClassFildes.ASPHALT);
        for(int j=0;j<storedProcedure.size();j++){
            redisTemplate.opsForHash().put(ErpProperties.WECHAT_STOCK+ss.format(yesterday),storedProcedure.get(j).getStockCode(),storedProcedure.get(j).getStockOnHand());
        }
    }
}
